<?php

    $hostname = gethostname();
    if ($hostname == "just68.justhost.com") 
    {
	    $username = 'kurtoric_user';
	    $password = 'DB_Admin';
	    $DBName = 'kurtoric_hpluserdata';
	}
    else
    {
        $username = 'root';
	    $password = '';
	    $DBName = 'hpluserdata';
    }
	
    $host = 'localhost';
    $error = 'Could not connect to DB. Check yourself';
    
    try
	{
		@mysql_connect($host, $username, $password) or die($error);
		$db_selected = @mysql_select_db($DBName);
        if (!$db_selected) {
            $sql_create_db = "CREATE DATABASE $DBName";
            mysql_query($sql_create_db);
            @mysql_select_db($DBName);
        }
	}
	catch(PDOException $e)
	{
		echo $error . ': ' . $e->getMessage();
	}
	//create a session ID for user visit
	$ses_id = session_id();
	if(empty($ses_id)) session_start();

	
	//Check for tables existence, or create them
	$sql_check = "SELECT * FROM dads";
	$sql_result = mysql_query($sql_check);
	if ( !$sql_result )
	{
		$sql_create_users = "CREATE TABLE users (idNum INT, PRIMARY KEY(idNum), sex TEXT, firstName TEXT, lastName TEXT, visit TEXT, streetAddress TEXT, city TEXT, age INT, occupation TEXT, retired BOOLEAN, haskids BOOLEAN, status TEXT, kids INT, Allergies TEXT, Sensitivities TEXT)";
		$sql_create_habits = "CREATE TABLE habits (idNum INT, PRIMARY KEY(idNum), alcohol BOOLEAN, drinkingfreq TEXT, druguse BOOLEAN, drugtype TEXT, usingfreq TEXT, smoking TEXT, numSigsPerDay INT, smokingQuantity TEXT, yearsSmoking INT, quitsmoke BOOLEAN, yearsquit INT, activity TEXT)";
		$sql_create_background = "CREATE TABLE background (idNum INT, PRIMARY KEY(idNum), operated BOOLEAN, diseased BOOLEAN, diseases TEXT, timeill INT, timeUnits TEXT, treated BOOLEAN)";
		$sql_create_moms = "CREATE TABLE moms (idNum INT, PRIMARY KEY(idNum), alive BOOLEAN, deathAge INT, deathCause TEXT, background BOOLEAN, diseases TEXT)";
		$sql_create_dads = "CREATE TABLE dads (idNum INT, PRIMARY KEY(idNum), alive BOOLEAN, deathAge INT, deathCause TEXT, background BOOLEAN, diseases TEXT)";
		$sql_create_siblings = "CREATE TABLE siblings (idNum INT, PRIMARY KEY(idNum), healthy BOOLEAN, diseases TEXT, death BOOLEAN, gender TEXT, deathAge INT, deathCause TEXT)";
		$sql_create_sessions = "CREATE TABLE sessions (sessionID TEXT NOT NULL, date DATETIME, idNum INT)";
		$sql_create_complaints = "CREATE TABLE complaints (complaint_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (complaint_id), idNum INT, sessionID TEXT NOT NULL, complaint TEXT NOT NULL, pending BOOLEAN)";
		$sql_create_temperature = "CREATE TABLE temperature (idNum INT, sessionID TEXT NOT NULL, maxFever TINYINT, feverDuration TINYINT, DurationUnits TEXT, sideEffects TEXT, feverStart TEXT, feverType TEXT, reaction BOOLEAN)";
		$sql_create_pain = "CREATE TABLE pain (idNum INT, sessionID TEXT NOT NULL, Location TEXT, addBodyPain BOOLEAN, Duration INT, DurationUnits TEXT, Type TEXT, Characterization TEXT, foodConection TEXT, bowelConection TEXT, priorMedication BOOLEAN, relieve BOOLEAN, Easer TEXT, Projects BOOLEAN, Projection TEXT, recurring BOOLEAN)";
		$sql_create_diarrhea = "CREATE TABLE diarrhea (idNum INT, sessionID TEXT NOT NULL, Duration TINYINT, DurationUnits TEXT, additions TEXT, addBlood BOOLEAN, bloodChar TEXT, bloodQuantity TEXT, volume TEXT, numPerDay TINYINT, daynight TEXT, foodRelation TEXT, constantNeed BOOLEAN, addFever BOOLEAN, feverHeight TINYINT, medicationResponse BOOLEAN, feverAttackTime TEXT, addNausea BOOLEAN, addDizzyness BOOLEAN, addVomit BOOLEAN, vomitNum TINYINT, vomitContent TEXT, medicated BOOLEAN, recurring BOOLEAN, othersSick BOOLEAN)";
        $sql_create_vomating = "CREATE TABLE vomating (idNum INT, sessionID TEXT NOT NULL, duration TINYINT, durationUnits TEXT, frequency TINYINT, frequencyUnits TEXT, content TEXT, hasNausea BOOLEAN, hasDizzi BOOLEAN, hasBurn BOOLEAN, foodRelated BOOLEAN, hasHiccup BOOLEAN, hasFever TINYINT, feverBehavior TEXT, hasShiver BOOLEAN, tookMedication BOOLEAN, hadBefore BOOLEAN, otherPerson BOOLEAN)";
        $sql_create_burn = "CREATE TABLE burn (idNum INT, sessionID TEXT NOT NULL, duration TINYINT, durationUnits TEXT, started BOOLEAN, withTaste BOOLEAN, foodRelated TEXT, time TEXT, swallow BOOLEAN, pastProblems TEXT, hasHiccup BOOLEAN, hasSwelling BOOLEAN, hasGas BOOLEAN, hasCoughing BOOLEAN, hasHoarseness BOOLEAN, hadFoodChanges BOOLEAN, hadSurgery BOOLEAN, hasHiatusHernia BOOLEAN, tookFoodAdditive BOOLEAN, hadHappendBefore BOOLEAN, happend TINYINT, happendUnits TEXT, hadGastroCheck BOOLEAN, gastroCheck TINYINT, gastroCheckUnits TEXT)";
		$sql_create_hiccup = "CREATE TABLE hiccup (idNum INT, sessionID TEXT NOT NULL, duration TINYINT, durationUnits TEXT, started BOOLEAN, withTaste BOOLEAN, foodRelated TEXT, swallow BOOLEAN, pastProblems TEXT, devState BOOLEAN, hasSwelling BOOLEAN, hasGas BOOLEAN, hasBrun BOOLEAN, foodChanges BOOLEAN, tookFoodAdditive BOOLEAN, hadHappendBefore BOOLEAN, happend TINYINT, happendUnits TEXT)";
		
		mysql_query($sql_create_users);
		mysql_query($sql_create_habits);
		mysql_query($sql_create_background);
		mysql_query($sql_create_moms);
		mysql_query($sql_create_dads);
		mysql_query($sql_create_siblings);
		mysql_query($sql_create_sessions);
		mysql_query($sql_create_complaints);	
		mysql_query($sql_create_temperature);
		mysql_query($sql_create_pain);
		mysql_query($sql_create_diarrhea);
		mysql_query($sql_create_constipation);
        mysql_query($sql_create_burn);
        mysql_query($sql_create_vomating);
        mysql_query($sql_create_hiccup);
	}	
?>